/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.List;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import model.Employee;
import model.Employeeproject;
import session.EmployeeFacadeRemote;
import session.EmployeeprojectFacadeRemote;
//import sun.text.normalizer.UTF16;
import util.Constants;

/**
 *
 * @author Aqua
 */
public class employeeProjectController {
    
   public static EmployeeprojectFacadeRemote getEmployeeProjectProject() throws NamingException{
        return util.Facade.getEmployeeProjectFacade();
    }
   public static String getActionLink(String id) {
        String delete = "<a onclick=\"doAction(frmIndex, '" + Constants.ADMIN_PAGE_NAME +
                "', '" + Constants.PROJECT_MANAGEMENT_PAGE_NAME+ "', '" + Constants.PROJECT_EMPLOYEE_LIST +
                "', '" + Constants.DELETE +"', '" + id + "')\">Delete</a>";
        return delete;
    }
   public static void processAction(HttpServletRequest request) {
        String action = request.getParameter("action");
        String selectedID = request.getParameter("Project");
        Controller.log.info(selectedID);
        Controller.log.info("employeeProjectController.processAction");
        try{
            if(Constants.SHOW_LIST.equalsIgnoreCase(action)){
                
                //process show project employee list action
                
                if((selectedID == null) || "".equals(selectedID)) {
                    //If the selectedID is null, throw an Exception.
                    Controller.sendMessage(request, "Please choose the project", true);
                    throw new Exception("No selected id to delete.");
                    
                } else {
                    HttpSession session = request.getSession();
                    session.setAttribute("projectid", selectedID);
                }
            }else if(Constants.DELETE.equalsIgnoreCase(action)){
                
                //process delete an employee in target project
                util.Facade.getEmployeeProjectFacade().deleteByID(selectedID);
                Controller.sendMessage(request, "Delected: ", true);
//                if((selectedID == null) || "".equals(selectedID)) {
//                    //If the selectedID is null, throw an Exception.
//                    Controller.sendMessage(request, "Please choose the project", true);
//                    throw new Exception("No selected id to delete.");
//                } else{
//                
//                }
            }      
        } catch (Exception ex) {
            Controller.log.error("Error EmployeeProjectController.processAction" + ex.getMessage());
        }
   }
   public static void showEmployeeListByID(JspWriter out, String id){
       try{
            List<model.Employeeproject> employeeprojectList = getEmployeeProjectProject().findListEmployeeByProjectID(id);
            //Show employee list on screen
            //create title
            out.println("<div id='list-table'>");
            out.println("<div class='row'>");
            out.println("<div class='column-header' style='width: " +
                    Constants.ID_COLUMN_SIZE +";'>ID</div>");
            out.println("<div class='column-header' style='width: " +
                    Constants.OBJECT_NAME_COLUMN_SIZE +";'>Name</div>");
            out.println("<div class='column-header' style='width: " +
                    Constants.DATE_COLUMN_SIZE +";'>Joined Day</div>");
            out.println("<div class='column-header' style='width: " +
                    Constants.ACTION_COLUMN_SIZE +";'>Action</div>");
            out.println("<div class='clear'></div>");
            out.println("</div>");
            
            //show value
            
            for(int i = 0; i < employeeprojectList.size(); i++) {
                model.Employeeproject node = employeeprojectList.get(i);
                
                out.println("<div class='row'>");
                out.println("<div class='column' style='width: " +
                        Constants.ID_COLUMN_SIZE +";'>" +
                        node.getEmployee().getEmployeeID() + "</div>");
                out.println("<div class='column' style='width: " +
                        Constants.OBJECT_NAME_COLUMN_SIZE +";'>" +
                        node.getEmployee().getShortName()+ "</div>");
                out.println("<div class='column' style='width: " +
                        Constants.DATE_COLUMN_SIZE +";'>" +
                        node.getJoinedDate() +"</div>");
                out.println("<div class='column' style='width: " +
                        Constants.ACTION_COLUMN_SIZE +";'>" +
                        getActionLink(node.getEmployeeprojectPK().toString()) +"</div>");
                out.println("<div class='clear'></div>");
                out.println("</div>");
            }
       } catch(Exception ex) {
            Controller.log.error("Error showAllEmployeeList: " + ex.getMessage());
       }
   }
   
   public static void showEmployeeRemainByID(){
       //show employees is not in project
   }
}
